import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
  OnInit,
  Output,
  EventEmitter,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  GetTabDtosForTabOutput,
  GetTechDocMaterialDto,
  MaterialChargeListDto,
  MaterialChargeServiceProxy,
  MonthPlanServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/primeng";
import { ModalDirective } from "ngx-bootstrap";
@Component({
  selector: "boundReadymaterial",
  templateUrl: "./bound-readymaterial-modal.component.html",
  styleUrls: ["./bound-readymaterial-modal.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class BoundReadymaterialModalComponentComponent extends AppComponentBase {
  @ViewChild("dataTable") dataTable: Table;
  @ViewChild("paginator1") paginator1: Paginator;
  @ViewChild("actionModal") modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  tabType = 1;

  tabs: GetTabDtosForTabOutput[];
  advancedFiltersAreShown = false;

  anyncing = false;
  active = false;
  saving = false;
  monthId: string;
  techDocCode: string;
  event1?: LazyLoadEvent;
  filterText = "";
  selectedColumns: any[];
  cols: any[];
  charges = new Array<MaterialChargeListDto>();
  expandedRowKeys = [];
  selected = [];
  allcharges = new Array<MaterialChargeListDto>();
  constructor(
    private _materialChargeServiceProxy: MaterialChargeServiceProxy,
    private _monthServiceProxy: MonthPlanServiceProxy,
    injector: Injector
  ) {
    super(injector);
  }

  show(month: string, techDocCode: string, event1?: LazyLoadEvent) {
    this.active = true;
    this.monthId = month;
    this.techDocCode = techDocCode;
    this.event1 = event1;
    this.allcharges = new Array<MaterialChargeListDto>();
    this.modal.show();
    setTimeout(() => {
      this.getmaterial();
    }, 1000);
  }

  getmaterial(event?: LazyLoadEvent) {
    // if (this.primengTableHelper.shouldResetPaging(event)) {
    //   this.paginator.changePage(0);
    //   return;
    // }

    this.primengTableHelper.showLoadingIndicator();

    this._materialChargeServiceProxy
      .getMaterialCharges(
        this.filterText,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        5,
        this.primengTableHelper.getSkipCount(this.paginator1, event)
      )
      .subscribe((result) => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.charges = result.items;

        this.charges.forEach((el) => {
          this.allcharges.push(el);
        });

        this.primengTableHelper.hideLoadingIndicator();
      });
  }
  save() {
    this.saving = true;
    var input = new Array<GetTechDocMaterialDto>();
    this.selected.forEach((el) => {
      var m = new GetTechDocMaterialDto();
      var am = this.allcharges.find((p) => p.id == el);
      m.grade = am.paiHao;
      m.name = am.materialName;
      m.size = am.materialSize;
      m.materialStatus = am.materialSpec;
      m.monthId = this.monthId;

      input.push(m);
    });
    this._monthServiceProxy.updateMaterial(input).subscribe((result) => {
      this.saving = false;
      this.notify.success("绑定成功");
      this.close();
      this.modalSave.emit(this.event1);
    });
  }
  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
